/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmQuanLyLop.java
 *
 * Created on Nov 24, 2011, 10:43:52 PM
 */
package GUI;

import DAO.Connect;
import DatabaseAccess.quanLyLop;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dell
 */
public class FrmQuanLyLop extends javax.swing.JFrame {
    DefaultTableModel model;
    Connection conn;
    Statement st;
    PreparedStatement pstmt;
    ResultSet rs;
    quanLyLop nhap = new quanLyLop();
    Connect connect = new Connect();
    public static String ClassIDD,ClassIDE,ClassID;
    /** Creates new form FrmQuanLyLop */
    public FrmQuanLyLop() {
        initComponents();
        model = (DefaultTableModel) tblClass.getModel();
        nhap.Load(tblClass);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblQLL = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblClass = new javax.swing.JTable();
        cboClass = new javax.swing.JComboBox();
        txtSearchClass = new javax.swing.JTextField();
        btnEditClass = new javax.swing.JButton();
        btnDetailClass = new javax.swing.JButton();
        btnAddActive = new javax.swing.JButton();
        btnSearchClass = new javax.swing.JButton();
        btnAddClass = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Manager Class");

        lblQLL.setFont(new java.awt.Font("Tahoma", 1, 36));
        lblQLL.setForeground(new java.awt.Color(204, 0, 0));
        lblQLL.setText("Manager Class");

        tblClass.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ClassID", "ClassName", "AgeGroupID"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblClass);

        cboClass.setFont(new java.awt.Font("Tahoma", 0, 12));
        cboClass.setForeground(new java.awt.Color(0, 0, 204));
        cboClass.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "Name", " " }));

        txtSearchClass.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtSearchClass.setForeground(new java.awt.Color(0, 0, 204));

        btnEditClass.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnEditClass.setForeground(new java.awt.Color(0, 0, 204));
        btnEditClass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/comment.png"))); // NOI18N
        btnEditClass.setText("Edit");
        btnEditClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditClassActionPerformed(evt);
            }
        });

        btnDetailClass.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDetailClass.setForeground(new java.awt.Color(0, 0, 204));
        btnDetailClass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/full_page.png"))); // NOI18N
        btnDetailClass.setText("Detail");
        btnDetailClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetailClassActionPerformed(evt);
            }
        });

        btnAddActive.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAddActive.setForeground(new java.awt.Color(0, 0, 204));
        btnAddActive.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add_page.png"))); // NOI18N
        btnAddActive.setText("Add Active");
        btnAddActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActiveActionPerformed(evt);
            }
        });

        btnSearchClass.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnSearchClass.setForeground(new java.awt.Color(0, 0, 204));
        btnSearchClass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/search.png"))); // NOI18N
        btnSearchClass.setText("Search");
        btnSearchClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchClassActionPerformed(evt);
            }
        });

        btnAddClass.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAddClass.setForeground(new java.awt.Color(0, 0, 204));
        btnAddClass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btnAddClass.setText("Add");
        btnAddClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddClassActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(138, 138, 138)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cboClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(txtSearchClass, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblQLL, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(btnSearchClass)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(108, Short.MAX_VALUE)
                .addComponent(btnDetailClass)
                .addGap(28, 28, 28)
                .addComponent(btnEditClass)
                .addGap(27, 27, 27)
                .addComponent(btnAddClass)
                .addGap(18, 18, 18)
                .addComponent(btnAddActive)
                .addGap(98, 98, 98))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblQLL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSearchClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnSearchClass))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDetailClass)
                    .addComponent(btnEditClass)
                    .addComponent(btnAddClass)
                    .addComponent(btnAddActive))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-671)/2, (screenSize.height-392)/2, 671, 392);
    }// </editor-fold>//GEN-END:initComponents

private void btnSearchClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchClassActionPerformed
// TODO add your handling code here:
     //Xóa dữ liệu trong data Vector object
     //Xóa dữ liệu trong data Vector object
    model.getDataVector().removeAllElements();
    //Cập nhật lại jTable để hiển thị trên màn hình
    tblClass.repaint();
    if (txtSearchClass.getText().length() == 0) {
        JOptionPane.showMessageDialog(null, "Dien vao tu khoa can tim!");
        return;
    }
    if (txtSearchClass.getText().equals("all")) {
        nhap.Load(tblClass);
    }
    if (cboClass.getSelectedItem().toString().equals("ID")) {

        try {
            conn = connect.getConnection();
            String strSQL = "select ClassID,ClassName,AgeGroupID from tblClass where ClassID=?";
            Vector v = new Vector();


            pstmt = conn.prepareStatement(strSQL);
            pstmt.setString(1, txtSearchClass.getText());
            rs = pstmt.executeQuery();
            try {

                while (rs.next()) {
                    v = new Vector();
                    v.addElement(rs.getString(1));
                    v.addElement(rs.getString(2));
                    v.addElement(rs.getString(3));
                    model.addRow(v);
                }
                //set lai model cho jtable
                tblClass.setModel(model);
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error:" + ex.getMessage());
            return;
        }
    }
    if (cboClass.getSelectedItem().toString().equals("Name")) {
        try {
            conn = connect.getConnection();
            String strSQL = "select ClassID,ClassName,AgeGroupID from tblClass where ClassName like ?";
            Vector v = new Vector();


            pstmt = conn.prepareStatement(strSQL);
            pstmt.setString(1, "%" + txtSearchClass.getText() + "%");
            rs = pstmt.executeQuery();
            try {

                while (rs.next()) {
                    v = new Vector();
                    v.addElement(rs.getString(1));
                    v.addElement(rs.getString(2));
                    v.addElement(rs.getString(3));
                    model.addRow(v);
                }
                //set lai model cho jtable
                tblClass.setModel(model);
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error:" + ex.getMessage());
            return;
        }
    }
                          
}//GEN-LAST:event_btnSearchClassActionPerformed

private void btnDetailClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetailClassActionPerformed
// TODO add your handling code here:
     int n = tblClass.getSelectedRow();

    if (n == -1) {
        JOptionPane.showMessageDialog(null, "Chon lop can xem!");
        return;
    }
    ClassIDD = tblClass.getValueAt(n, 0).toString();

    FrmDetailClass frmdetailclass = new FrmDetailClass();
    frmdetailclass.setVisible(true);
}//GEN-LAST:event_btnDetailClassActionPerformed

private void btnEditClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditClassActionPerformed
// TODO add your handling code here:
    int m = tblClass.getSelectedRow();
    if (m == -1) {
        JOptionPane.showMessageDialog(this, "Chon du lieu can sua!");
        return;
    }
    ClassIDE = tblClass.getValueAt(m, 0).toString();
    FrmEditClass frmeditclass = new FrmEditClass();
    frmeditclass.setVisible(true);
}//GEN-LAST:event_btnEditClassActionPerformed

private void btnAddClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddClassActionPerformed
// TODO add your handling code here:

     FrmAddClass frmaddclass = new FrmAddClass();
    frmaddclass.setVisible(true);
}//GEN-LAST:event_btnAddClassActionPerformed

private void btnAddActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActiveActionPerformed
// TODO add your handling code here:
            int h = tblClass.getSelectedRow();
    if (h == -1) {
        JOptionPane.showMessageDialog(this, "Choose a Class to Add");
        return;
    }
    ClassID = tblClass.getValueAt(tblClass.getSelectedRow(), 0).toString();
     FrmSelectActiveforClass frmclass = new FrmSelectActiveforClass();
    frmclass.setVisible(true);
}//GEN-LAST:event_btnAddActiveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmQuanLyLop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmQuanLyLop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmQuanLyLop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmQuanLyLop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmQuanLyLop().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddActive;
    private javax.swing.JButton btnAddClass;
    private javax.swing.JButton btnDetailClass;
    private javax.swing.JButton btnEditClass;
    private javax.swing.JButton btnSearchClass;
    private javax.swing.JComboBox cboClass;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblQLL;
    private javax.swing.JTable tblClass;
    private javax.swing.JTextField txtSearchClass;
    // End of variables declaration//GEN-END:variables
}
